/*
 * Created: Author: Aléx Carvalho / Date: Mar 4, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-12 00:38:01 -0300 (Fri, 12 Jun 2015) $
 */
package com.cpm.view.zk.field;

import com.cpm.view.Element;
import com.cpm.wrapper.AttributeMap;
import com.cpm.wrapper.SolidObjects;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.zkoss.zk.ui.AbstractComponent;

public abstract class AbstractInputField<VALUE> implements Element<AbstractComponent> {

    private String label;
    private boolean required = false;

    private final AttributeMap attributeMap = new AttributeMap();
    private final Set<FieldValidator> fieldValidators = new HashSet<>();

    public abstract void setValue(VALUE value);

    public abstract VALUE getValue();

    public abstract boolean isEnabled();

    public abstract void setEnabled(boolean enable);

    @Override
    public abstract AbstractComponent getElement();

    public void setLabel(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public final void putAttribute(Object key, Object value) {
        attributeMap.putAttribute(key, value);
    }

    public final Object getAttribute(Object key) {
        return attributeMap.getAttribute(key);
    }

    public final void removeAttribute(Object key) {
        attributeMap.removeAttribute(key);
    }

    public final boolean isRequired() {
        return this.required;
    }

    public final void setRequired(boolean required) {
        this.required = required;
    }

    public final void addFieldValidator(FieldValidator... validators) {
        SolidObjects.notNull((Object) validators);

        Collections.addAll(fieldValidators, validators);
    }

    public final boolean isValid() {
        boolean value = true;

        for (FieldValidator fieldValidator : fieldValidators) {
            value = value && fieldValidator.validate(getValue());
        }

        return value;
    }
}
